//
//  SellerMineViewController.m
//  SmartCoin
//
//  Created by apple on 2018/3/7.
//  Copyright © 2018年 Cynic. All rights reserved.
//

#import "SellerMineViewController.h"
#import "SellerMineOperationTableViewCell.h"
#import "SellerSettingViewController.h"
#import "MyNotificationViewController.h"
#import "SellerMainViewController.h"
#import "CashOutViewController.h"
#import "DetailBuyyerViewController.h"
#import "BuyyerMainViewController.h"
#import "SuggestionViewController.h"
static NSString *SellerMineOperationTableViewCellIndentifier = @"SellerMineOperationTableViewCellIndentifier";
@interface SellerMineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,retain)UIView *PerSonPartView;
@property (nonatomic ,retain)UIButton *header;
@property (nonatomic ,retain)UILabel *joblabel;
@property (nonatomic ,retain)UILabel *idlabel;
@property (nonatomic ,retain)NSArray *operations;
@property (nonatomic ,retain)UILabel *namelabel;
@property (nonatomic ,assign)BOOL isseller;//是商户
//@property (nonatomic ,copy)NSString *shopID;
//@property (nonatomic ,retain)NSMutableDictionary *personData;
@end

@implementation SellerMineViewController

-(instancetype)initWithSeller{
    self = [super initInChildWithFrame:CGRectMake(0, cxsingler.topdis - cxsingler.navitop, cx_wid, cx_hit - cxsingler.topdis - cxsingler.bottomdis - 50 + cxsingler.navitop)];
//    self.view.frame = CGRectMake(0, cxsingler.topdis, cx_wid, cx_hit - cxsingler.topdis - cxsingler.bottomdis - 50);
    _isseller = YES;
//    [self selfSellterInfoLoad];
    [self viewLoad];
    return self;
}

-(instancetype)initWithshopID:(NSString *)shopID{
    self = [super initInChildWithFrame:CGRectMake(0, cxsingler.topdis - cxsingler.navitop, cx_wid, cx_hit - cxsingler.topdis - cxsingler.bottomdis - 50 + cxsingler.navitop)];
    [self dataLoad];
//    self.view.frame = CGRectMake(0, cxsingler.topdis, cx_wid, cx_hit - cxsingler.topdis - cxsingler.bottomdis - 50);
    _isseller = NO;
    [self viewLoad];
    return self;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = bggrayColor;
//    [self viewLoad];
    // Do any additional setup after loading the view.
}

-(void)dataLoad{
    NetWorkModel *netModel = [[NetWorkModel alloc] init];
    WEAKSELF;
    netModel.sendMessage = ^(NSString *result) {
        if ([result isEqual:@"success"]) {
            NSMutableDictionary *placeDic = [cxsingler.DicData mutableCopy];
            NSArray *keys = [placeDic allKeys];
            for (NSInteger index = 0; index < keys.count; index ++) {
                NSString *key = keys[index];
                [cxsingler.personData setObject:[weakSelf Nnll:placeDic[key] withPlace:@"--"] forKey:key];
                [cxsingler.loginrequest setObject:[weakSelf Nnll:placeDic[key] withPlace:@"--"] forKey:key];
            }
            [weakSelf personinfoset];
        }
        else{
            [SVProgressHUD showErrorWithStatus:result];
        }
    };
    [netModel getUserMoneyinfoByUser:cxsingler.staffid];
}

-(void)viewLoad{
    _operations = @[@[@"我的通知",@"header_notice-1"],@[@"我的分红",@"header_share"],@[@"提现",@"header_finance"],@[@"帮助与反馈",@"header_help"],];
    [self PerSonPartLoad];
    [self navigationLoad];
}

-(void)navigationLoad{
//    CGFloat topchange = 0;
//    if (_isseller) {
//        if (cxsingler.topdis > 0) {
//        topchange = -44;
//        }
//    }
    UIButton *popbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0 , 44, 44)];
    NSLog(@"%lf-=-=-=-=-=-%lf",cxsingler.topdis,popbtn.frame.origin.y);
    [popbtn setImage:[UIImage imageNamed:@"icon_change"] forState:UIControlStateNormal];
    [popbtn addTarget:self action:@selector(popbtnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *rightbtn = [[UIButton alloc] initWithFrame:CGRectMake(cx_wid - 44,  0 , 44, 44)];
    [rightbtn addTarget:self action:@selector(rightbtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [rightbtn setImage:[UIImage imageNamed:@"icon_set"] forState:UIControlStateNormal];
    [self.view addSubview:popbtn];
    [self.view addSubview:rightbtn];
}

-(void)personinfoset{
    /*
     avatar = "<null>";
     balance = 0;
     bonusAmount = 0;
     empno = cx001;
     id = 597;
     jobBonusState = 1;
     jobId = 81;
     jobRate = "5.555552";
     mobile = 18012345678;
     shopId = 51;
     withdrawalAmount = 0;
     */
    dispatch_async(dispatch_get_main_queue(), ^{
        [self setpersoninfo];
    });
}

-(void)PerSonPartLoad{
    CGFloat hit = 240.0 / 375 * cx_wid;
//    CGFloat topdis = _isseller ? cxsingler.topdis :
    CGFloat topchange = 0;
//    if (_isseller) {
////        if (cxsingler.topdis > 0) {
////            topchange = -44;
////        }
//    }
    if (!_PerSonPartView) {
        _PerSonPartView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cx_wid, hit)];
        UIImageView *headBG = [[UIImageView alloc] initWithFrame:_PerSonPartView.bounds];
        headBG.image = [UIImage imageNamed:@"背景3"];
        [_PerSonPartView addSubview:headBG];
        
        _header = [[UIButton alloc] init];
        CGFloat wid = 68.0 / 240 * hit;
        _header.bounds = CGRectMake(0, 0, wid  , wid);
        _header.center = CGPointMake(cx_wid / 2, 128.0 / 240 * hit);
        _header.layer.cornerRadius = wid / 2;
        _header.layer.borderWidth = 3;
        _header.backgroundColor = bggrayColor;
        _header.layer.borderColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.4].CGColor;
        _header.clipsToBounds = YES;

        [_PerSonPartView addSubview:_header];
        
        _namelabel = [[UILabel alloc] init];
        _namelabel.bounds = CGRectMake(0, 0, cx_wid - 50, 22);
        _namelabel.center = CGPointMake(cx_wid / 2, 180.0 / 240 * hit);
        _namelabel.textAlignment = NSTextAlignmentCenter;
        _namelabel.textColor = [CXSinglerModel colorWithHex:@"#4A4A4A" alpha:1];
        _namelabel.font = [UIFont systemFontOfSize:16];
        _namelabel.text = @"--";
        [_PerSonPartView addSubview:_namelabel];
        
        NSArray *infos = @[@[@"skill_color",@"--"],@[@"id",@"--"]];
        for (NSInteger index = 0; index < 2; index ++) {
            UIView *partview = [[UIView alloc]init];
            
            partview.bounds = CGRectMake(0, 0, 110, 20);
            partview.center = CGPointMake(cx_wid / 2 - 60 + 120 * index, 207.0 / 240 * hit);
            
            UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 20, 20)];
            [partview addSubview:icon];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 80, 20)];
            [partview addSubview:label];
            if (index == 0) {
                _joblabel = label;
            }
            else if (index == 1){
                _idlabel = label;
            }
            label.text = infos[index][1];
            icon.image = [UIImage imageNamed:infos[index][0]];
            label.textColor = [CXSinglerModel colorWithHex:@"#9B9B9B" alpha:1];
            label.font = [UIFont systemFontOfSize:14];
            [_PerSonPartView addSubview:partview];
        }
        [self.view addSubview:_PerSonPartView];
        [self personinfoset];
    }
    
//    CGFloat topchange = 0;
//    if (_isseller) {
//        topchange = -44;
//    }
    UITableView *mainView = [[UITableView alloc] initWithFrame:CGRectMake(0, 250.0 / 240 * hit + cxsingler.topdis + topchange - cxsingler.navitop, cx_wid, cx_hit - cxsingler.topdis - cxsingler.bottomdis - 50 - 250.0 / 240 * hit + cxsingler.navitop)];
    mainView.estimatedRowHeight = 0;
    mainView.backgroundColor = [UIColor clearColor];
    mainView.delegate = self;
    mainView.dataSource = self;
    mainView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    mainView.backgroundColor = [UIColor whiteColor];
    mainView.scrollEnabled = NO;
    [mainView registerClass:[SellerMineOperationTableViewCell class] forCellReuseIdentifier:SellerMineOperationTableViewCellIndentifier];
    [self.view addSubview:mainView];
}

//-(void)selfSellterInfoLoad{
//    NetWorkModel *netmodel = [[NetWorkModel alloc] init];
//    [netmodel getSllerInfo];
//    WEAKSELF;
//    netmodel.sendMessage = ^(NSString *result) {
//        if ([result isEqual:@"success"]) {
//        NSDictionary *partdic = cxsingler.DicData;
//        NSArray *list = [partdic allKeys];
//        for (NSString *key in list) {
//            NSLog(@"%@ ----- %@",[partdic[key]  class],key);
//            [cxsingler.loginrequest setObject:[self Nnll:partdic[key] withPlace:@""] forKey:key];
//        }
//        [weakSelf setpersoninfo];
//   
//    }
//    else{
//        [SVProgressHUD showErrorWithStatus:result];
//    }
//         };
//}

-(void)setpersoninfo{
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString *headerUrl = [NSString stringWithFormat:@"%@",cxsingler.loginrequest[@"logo"]];
            if ([headerUrl containsString:@"gif"]) {
                [_header setImage:[UIImage sd_animatedGIFWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:headerUrl]]] forState:UIControlStateNormal];
            }
            else{
            [_header sd_setBackgroundImageWithURL:[NSURL URLWithString:headerUrl] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"logo1"]];
            }
            if(_isseller){
                _namelabel.text = [self Nnll:cxsingler.loginrequest[@"name"] withPlace:@"--"];
                _joblabel.text = [self Nnll:cxsingler.loginrequest[@"role"] withPlace:@"--"];
                _idlabel.text = [self Nnll:[NSString stringWithFormat:@"%@",cxsingler.loginrequest[@"admin"][@"shop_id"]] withPlace:@"--"];
            }
            else{
            _namelabel.text = [self Nnll:cxsingler.loginrequest[@"name"] withPlace:@"--"];
            _joblabel.text = [self Nnll:cxsingler.jobname withPlace:@"--"];
            _idlabel.text = [self Nnll:cxsingler.personData[@"empno"] withPlace:@"--"];
            }
        });
}

#pragma mark--uitableview代理开始
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _operations.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger sec = indexPath.section;
    if (_isseller){
        if (sec == 3) {
            return 54;
        }
        else{
            return 0;
        }
    }
    if (sec == 0 || sec == 3) {
        return 0;
    }
    return 54;
}

//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 10;
//}
//
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 10;
//}
-(UIView *)tableView:(UITableView *)tableView viewForfooterInSection:(NSInteger)section{
    UIView *headerview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cx_wid, 10)];
    headerview.backgroundColor = [UIColor clearColor];
    return headerview;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger sec = indexPath.section;
//    NSInteger row = indexPath.row;
    SellerMineOperationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SellerMineOperationTableViewCellIndentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.icon setImage:[UIImage imageNamed:_operations[sec][1]] forState:UIControlStateNormal];
//    cell.storelabel.text =  [NSString stringWithFormat:@"%@",partData[@"shopName"]];
//    cell.namelabel.text =  [NSString stringWithFormat:@"%@",partData[@"name"]];
//    cell.joblabel.text = [NSString stringWithFormat:@"%@",partData[@"jobTitle"]];
    cell.namelabel.text = _operations[sec][0];
//    cell.
    cell.clipsToBounds = YES;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger sec = indexPath.section;
    NSInteger row = indexPath.row;
    if (sec == 0) {
        MyNotificationViewController *nextVC = [[MyNotificationViewController alloc] init];
        [self.navigationController pushViewController:nextVC animated:YES];
    }
    else if (sec == 1){
        BuyyerMainViewController *nextVC = [[BuyyerMainViewController alloc] init];
        [self.navigationController pushViewController:nextVC animated:YES];
    }
    else if (sec == 2){
        CashOutViewController *nextVC = [[CashOutViewController alloc]init];
        [self.navigationController pushViewController:nextVC animated:YES];
    }
    else if (sec == 3){
        SuggestionViewController *nextVC = [[SuggestionViewController alloc] init];
        [self.navigationController pushViewController:nextVC animated:YES];
    }
}
#pragma mark--uitableview代理结束

#pragma mark--点击
-(void)popbtnClick:(UIButton *)sender{
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)rightbtnClick:(UIButton *)sender{
    [self.view endEditing:YES];
    SellerSettingViewController *nextVC = [[SellerSettingViewController alloc] init];
    [self.navigationController pushViewController:nextVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
